package com.axend.aerosense.user.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.user.adapter.LoginDevListAdapter;
import com.axend.aerosense.user.entity.LoginDevInfoBean;
import com.axend.aerosense.user.entity.LoginDevListBean;
import com.axend.aerosense.user.viewmodel.UserLoginDevsViewModel;
import com.axend.aerosense.user.R;
import com.axend.aerosense.user.UserSettingActivity;
import com.axend.aerosense.user.databinding.UserOnlineDevFragmentBinding;

import java.util.ArrayList;

public class UserLoginDevsFragment extends MvvmBaseFragment<UserOnlineDevFragmentBinding, UserLoginDevsViewModel>
    implements ILoadDataResult<LoginDevListBean> {
    private ArrayList<LoginDevInfoBean> deviceInfo = new ArrayList<LoginDevInfoBean>();
    private LoginDevListAdapter listAdapter;
    private LoginDevInfoBean selectItem = null;
    public static UserLoginDevsFragment getInstance(){
        UserLoginDevsFragment fragment = new UserLoginDevsFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString(ARG_PARAM, str);
//        fragment.setArguments(bundle);   //设置参数
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        ((UserSettingActivity)getActivity()).setTitle(getString(R.string.user_device));
    }

    private void initView() {
        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.LOGIN_DEV_CHANGE ,Boolean.class)
                .observe(getViewLifecycleOwner(), delete->{
                    if(delete){
                        listAdapter.getData().remove(selectItem);
                        String tmp = getString(R.string.user_online_dev) + " (" + listAdapter.getData().size() + ")";
                        viewDataBinding.userTitles.setText(tmp);
                        listAdapter.notifyDataSetChanged();
                        selectItem = null;
                    }
                });
        setLoadSir(viewDataBinding.userContentView);
        viewDataBinding.userDevList.setLayoutManager(new LinearLayoutManager(getContext()));
        listAdapter = new LoginDevListAdapter();
        viewDataBinding.userDevList.setAdapter(listAdapter);
        listAdapter.setOnDeleteItemClickListence(item -> {
            selectItem = item;
            changeFragment(SafeCheckFragment.getInstance(selectItem));

        });
        showLoading();
        viewModel.onLoad();
    }

    private void changeFragment(Fragment fragment){
        FragmentUtil.add(getActivity(),
                ((UserSettingActivity)getActivity()).getContentId(),
                fragment,
                this,
                true);
    }


    @Override
    public int getLayoutId() {
        return R.layout.user_online_dev_fragment;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected UserLoginDevsViewModel getViewModel() {
        BaseViewModelFactroy<LoginDevListBean> myViewModelFactroy = new BaseViewModelFactroy<>(this, this);
        UserLoginDevsViewModel tmp = new ViewModelProvider(this, myViewModelFactroy).get(UserLoginDevsViewModel.class);
        return tmp;
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void loadDataSuccess(LoginDevListBean loginDevListBean) {
        deviceInfo.clear();
        deviceInfo.addAll(loginDevListBean.getDeviceInfo());
        listAdapter.setList(deviceInfo);

        if(deviceInfo != null ){
            String tmp = getString(R.string.user_online_dev) + " (" + deviceInfo.size() + ")";
            viewDataBinding.userTitles.setText(tmp);
        }
    }

    @Override
    public void loadDataFail(String decs) {

    }

}
